import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import "../components"

Window {        // 分析配置


    signal accept()
    signal reject()

    property bool isFilter: true


    id: root
    title: "分析配置"
    color: "white"

    minimumHeight: 480*dp
    maximumHeight: 480*dp
    minimumWidth: 420*dp
    maximumWidth: 420*dp


    modality: Qt.WindowModal
    flags: Qt.Window | Qt.WindowTitleHint |Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint
    onVisibleChanged: {
        var model = CAnalysis.getModelConfig()
        combox_func.changeCurrentIndex(model["Func"])
        console.log("MODEL-Filter", model["Filter"])
        if(model["Filter"]===0){                // 为啥我要把这玩意设成2来着...
            combox_filter.changeCurrentIndex(0)
        }else if(model["Filter"]===2){
            combox_filter.changeCurrentIndex(1)
        }else{
            combox_filter.changeCurrentIndex(model["Filter"])
        }

        if(model["Ct"]===-1){
            combox_thr.changeCurrentIndex("1")
            combox_relaThr.editable = false
        }else{
            combox_thr.changeCurrentIndex("0")
            combox_relaThr.changeCurrentIndex(model["Ct"])
            combox_relaThr.editable = true
        }
    }


    Column{
        x: 50*dp
        y: 30*dp
        width: parent.width - 100*dp
        spacing: 5*dp
        Item{
            height: 80*dp
            width: parent.width
            Item{
                height: 40*dp
                width: parent.width
                Text{
                    x: 2
                    text: "原始曲线预处理"
                    font.pixelSize: config.fontsize
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            CustomRadio{
                id: radio_open
                title: "打开"
                isChecked: isFilter
                width: 60*dp
                x: parent.width - 130*dp
                onReleased: {
                    if(combox_filter.getText()===""){
                        combox_filter.changeCurrentIndex(0)
                    }
                    isFilter = true
                }
            }CustomRadio{
                id: radio_close
                title: "关闭"
                isChecked: !isFilter
                width: 60*dp
                x: parent.width - 60*dp
                onReleased: {
                    isFilter = false
                }
            }
            CustomComboBox{
                id: combox_filter
                y: 40*dp
                editWidth: parent.width
                model: ["三点均值", "五点二阶"]
                editable: isFilter
                Layout.alignment: Qt.AlignHCenter
            }
        }

        Item{
            width: parent.width
            height: 80*dp
            Item{
                height: 40*dp
                width: parent.width
                Text{
                    x: 2
                    text: "函数模型"
                    font.pixelSize: config.fontsize
                    anchors.verticalCenter: parent.verticalCenter
                }
            }CustomComboBox{
                id: combox_func
                y: 40*dp
                editWidth: parent.width
                model: ["Sigmoid", "Richards"]
            }
        }

        Item{
            width: parent.width
            height: 80*dp
            Item{
                height: 40*dp
                width: parent.width
                Text{
                    x: 2
                    text: "阈值线算法"
                    font.pixelSize: config.fontsize
                    anchors.verticalCenter: parent.verticalCenter
                }
            }

            CustomComboBox{
                id: combox_thr
                y: 40*dp
                editWidth: parent.width
                model: ["相对阈值", "绝对阈值"]
                Layout.alignment: Qt.AlignHCenter
                onCurrentText: {
                    if(text === "相对阈值"){
                        combox_relaThr.editable = true
                    }else{
                        combox_relaThr.editable = false
                    }
                }
            }
        }

        Item{
            width: parent.width
            height: 80*dp

            Item{
                height: 40*dp
                width: parent.width
                Text{
                    x: 2
                    text: "相对阈值方案"
                    font.pixelSize: config.fontsize
                    anchors.verticalCenter: parent.verticalCenter
                }
            }CustomComboBox{
                id: combox_relaThr
                y: 40*dp
                editWidth: parent.width
                model: ["CqMAN", "Cy0"]
                Layout.alignment: Qt.AlignHCenter
            }

        }
    }



    Item{
        height: 50*dp
        width: root.width
        y: parent.height - 90*dp
        PushButton{
            y: 10*dp
            x: root.width/2 - 130*dp
            title: "取消"
            defaultMode2: true
            onReleased: {
                reject()
                root.close()
            }
        }PushButton{
            y: 10*dp
            x: root.width/2 + 10*dp
            title: "确定"
            defaultMode1: true
            onReleased: {
                accept()
                var res = {
                    "Func": combox_func.getIndex(),
                    "Ct": combox_thr.getText()==="绝对阈值" ? -1 : combox_relaThr.getIndex(),
                    "Filter": isFilter ? (combox_filter.getIndex()===0 ? 0 : 2) : -1
                }
                CAnalysis.setModelConfig(res)
                root.close()
                root.accept()
            }
        }
    }

}
